package io.renren.modules.task.service.impl;

import com.google.common.collect.Lists;
import io.renren.common.utils.PageUtils;
import io.renren.modules.project.model.ProjectDo;
import io.renren.modules.task.mapper.ChinaMapper;
import io.renren.modules.task.model.ChinaVo;
import io.renren.modules.task.service.ChinaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


@Service("chinaService")
public class ChinaServiceImpl implements ChinaService {

    @Autowired
    private ChinaMapper chinaMapper;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        return null;
    }

    @Override
    public List<Map<String, Object>> getNodeTree() {
        List<ChinaVo> allList = chinaMapper.getAllList();
        List<Map<String, Object>> mapList = this.menuList(allList);
        mapList = this.cleanChild(mapList);
        return mapList;
    }

    private List<Map<String, Object>> menuList(List<ChinaVo> configList) {
        List<Map<String, Object>> mapList = Lists.newArrayList();
        //configCommon = configList;
        //排序
//        List<ProvCityDistVo> menuList = configList.stream().sorted(
//                Comparator.comparing(ProvCityDistVo::getParentId))
//                .collect(Collectors.toList());

        for (Object obj : configList) {
            Map<String, Object> mapArr = new LinkedHashMap<String, Object>();
            ChinaVo x = (ChinaVo) obj;
            if (x.getPid().equals(0)) {
                mapArr.put("id", x.getId());
                mapArr.put("name", x.getName());
                mapArr.put("pid", x.getPid());
                mapArr.put("children", menuChild(x.getId(), configList));
                mapList.add(mapArr);
            }
        }
        return mapList;
    }
    private List<?> menuChild(Integer id, List<?> roleMenuList) {
        List<Object> lists = new ArrayList<Object>();
        for (Object obj : roleMenuList) {
            ChinaVo a = (ChinaVo) obj;
            Map<String, Object> childArray = new LinkedHashMap<String, Object>();

            if (a.getPid().equals(id)) {
                childArray.put("id", a.getId());
                childArray.put("name", a.getName());
                childArray.put("pid", a.getPid());
                childArray.put("children", menuChild(a.getId(), roleMenuList));
                lists.add(childArray);
            }
        }
        return lists;
    }
    private List<Map<String, Object>> cleanChild(List<Map<String, Object>> mapList) {
        for (Map<String, Object> map : mapList) {
            List list = (List) map.get("children");
            if (list == null || list.size() <= 0) {
                map.remove("children");
            } else {
                cleanChild(list);
            }
        }
        return mapList;
    }

    @Override
    public List<ChinaVo> queryList() {
        return chinaMapper.getAllList();
    }

    @Override
    public ChinaVo getById(Long id) {
        ChinaVo byId = chinaMapper.getById(id);
        return byId;
    }

}